﻿using System;
using System.Globalization;
using System.Text.RegularExpressions;
using YanZhiwei.DotNet2.Utilities.Enums;

namespace YanZhiwei.DotNet2.Utilities.Common
{
    /// <summary>
    /// Date 帮助类
    /// </summary>
    public static class DateHelper
    {
        #region 获取日期是一年中第几个星期
        /// <summary>
        /// 获取日期是一年中第几个星期
        /// </summary>
        /// <param name="date">需要计算的时间</param>
        /// <returns>一年中第几个星期</returns>
        public static int GetWeekNumber(this DateTime date)
        {
            var _cultureInfo = CultureInfo.CurrentCulture;
            return _cultureInfo.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
        }
        #endregion
        #region 计算两个时间之间工作天数
        /// <summary>
        ///计算两个时间之间工作天数 
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>工作天数 </returns>
        public static int CountWeekdays(DateTime startTime, DateTime endTime)
        {
            TimeSpan _ts = endTime - startTime;
            int _weekCount = 0;
            for (int i = 0; i < _ts.Days; i++)
            {
                DateTime _time = startTime.AddDays(i);
                if (IsWeekDay(_time))
                {
                    _weekCount++;
                }
            }
            return _weekCount;
        }
        #endregion
        #region 计算两个时间直接周末天数
        /// <summary>
        ///计算两个时间直接周末天数 
        /// </summary>
        /// <param name="startTime">开始天数</param>
        /// <param name="endTime">结束天数</param>
        /// <returns>周末天数</returns>
        public static int CountWeekends(DateTime startTime, DateTime endTime)
        {
            TimeSpan _ts = endTime - startTime;
            int _weekendCount = 0;
            for (int i = 0; i < _ts.Days; i++)
            {
                DateTime dt = startTime.AddDays(i);
                if (IsWeekEnd(dt))
                {
                    _weekendCount++;
                }
            }
            return _weekendCount;
        }
        #endregion
        #region 是否是工作日
        /// <summary>
        /// 是否是工作日
        /// </summary>
        /// <param name="dt">DateTime</param>
        /// <returns>是否是工作日</returns>
        public static bool IsWeekDay(this DateTime dt)
        {
            return Convert.ToInt16(dt.DayOfWeek) < 6;
        }
        #endregion
        #region 是否是周末
        /// <summary>
        ///  是否是周末
        /// </summary>
        /// <param name="dt">DateTime</param>
        /// <returns>是否是周末</returns>
        public static bool IsWeekEnd(this DateTime dt)
        {
            return Convert.ToInt16(dt.DayOfWeek) > 5;
        }
        #endregion
        #region 日期差计算
        /// <summary>
        /// 日期差计算
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="flag">时间差枚举</param>
        /// <returns>时间差</returns>
        public static int DateDiff(this DateTime startTime, DateTime endTime, DatePart part)
        {
            int _resutl = 0;
            switch (part)
            {
                case DatePart.year:
                    _resutl = endTime.Year - startTime.Year;
                    break;
                case DatePart.month:
                    _resutl = (endTime.Year - startTime.Year) * 12 + (endTime.Month - startTime.Month);
                    break;
                case DatePart.day:
                    _resutl = (int)(endTime - startTime).TotalDays;
                    break;
                case DatePart.hour:
                    _resutl = (int)(endTime - startTime).TotalHours;
                    break;
                case DatePart.minute:
                    _resutl = (int)(endTime - startTime).TotalMinutes;
                    break;
                case DatePart.second:
                    _resutl = (int)(endTime - startTime).TotalSeconds;
                    break;
            }
            return _resutl;
        }
        #endregion
        #region 获取一个月有多少天
        /// <summary>
        /// 获取一个月有多少天
        /// </summary>
        /// <param name="date">时间</param>
        /// <returns>一个月有多少天</returns>
        public static int GetDays(this DateTime date)
        {
            return DateTime.DaysInMonth(date.Year, date.Month);
        }
        #endregion

    }
}
